package com.fare.fdc.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;
import javax.inject.Inject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.city.CityQueryApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.mycare.UserCareLineApplication;
import com.fare.taluo.auth.security.ContextLoginUser;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.base.struts2.json.BaseJsonStrutsAction;
import com.fare.taluo.base.struts2.json.JsonReturnBean;
import com.fare.taluo.exception.used.ValidateBusiDataException;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.param.ParamUtils;
import com.fare.taluo.vo.config.city.CityLineSelectVO;
import com.fare.taluo.vo.core.PortEntityVO;
import com.fare.taluo.vo.core.UserCareLineEntityVO;

@Controller("userCareLineAction")
@Scope("prototype")
@Namespace("/usercareline")
public class UserCareLineAction extends BaseJsonStrutsAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4620283316232119224L;
	@Inject
	private UserCareLineApplication userCareLineApplication;
	@Inject
	private SourceApplication sourceApplication;
	@Inject
	private CityQueryApplication cityQueryApplication;
	
	private Map<String,List<CityLineSelectVO>> lineMaps;
	
	private List<CityLineSelectVO> tempLines=new ArrayList<CityLineSelectVO>();
	
	private int flag;
	
	private String language;
	
	public Map<String,List<CityLineSelectVO>> getLineMaps() {
		return lineMaps;
	}

	/**
	 * 注册选择关注航线
	 * @return
	 */
	@Action(value="registerView",
			results={@Result(name="success", type="dispatcher", location="/pages/system/register/registeruser_careline.jsp")})
	public String registerView(){
		String hbaseKey = ParamUtils.getParameter(request, "hbaseKey");
		if(tempLines.size()==0 && flag !=1){
			tempLines = cityQueryApplication.getCitysByChannel(hbaseKey);
		}
		
		//国际化
		if(tempLines.size()>0){
			I18nUtil.cityCovert2I18N(tempLines, super.getLocale());
		}
		
		lineMaps = new TreeMap<String,List<CityLineSelectVO>>(new java.util.Comparator<String>(){
			@Override
			public int compare(String arg0, String arg1) {
				return arg0.compareTo(arg1);
			}
		});  
		//分好字母
		//只需要热门即可
		for(CityLineSelectVO vo : tempLines){
			if(vo.getIsHot()){
				if(lineMaps.get(vo.getFirstLet())==null)
					lineMaps.put(vo.getFirstLet(), new ArrayList<CityLineSelectVO>());
				lineMaps.get(vo.getFirstLet()).add(vo);
			}
		}
		//如果没有一个热门的时候，则全部加入
		if(lineMaps.isEmpty()){
			for(CityLineSelectVO vo : tempLines){
				if(lineMaps.get(vo.getFirstLet())==null)
					lineMaps.put(vo.getFirstLet(), new ArrayList<CityLineSelectVO>());
				lineMaps.get(vo.getFirstLet()).add(vo);
			}
		}
		return SUCCESS;
	}

	// 注册添加关注航线
	@Action(value="savelogin",
			results={@Result(name="success", type="json", params={"root","returnBean"})})
	public String savelogin()  {
		String checkedLine = ParamUtils.getParameter(request, "checkedLine");
		String userName = ContextLoginUser.getLoginUser().getLoginId();
		returnBean = new JsonReturnBean();
		try{
			userCareLineApplication.saveLogon(checkedLine, userName);
			returnBean.setHasOk(true);
		}catch(Exception e){
			returnBean.setHasOk(false);
			returnBean.setMessage(e.getMessage());
		}
		return SUCCESS;
	}

	@Resource(name="portApplication")
	private PortApplication portApplication;

	private Long id;
	private String depCity;
	private String arrCity;
	private List<UserCareLineEntityVO> datas;

	public List<UserCareLineEntityVO> getDatas() {
		return datas;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDepCity() {
		return depCity;
	}

	public void setDepCity(String depCity) {
		this.depCity = depCity;
	}

	public String getArrCity() {
		return arrCity;
	}

	public void setArrCity(String arrCity) {
		this.arrCity = arrCity;
	}
	/**
	 * 我的关注航线列表
	 * @return
	 */
	@Action(value="toView",
			results={@Result(name="success", type="dispatcher", location="/fare/fdc/careline.jsp")})
	public String toView(){
		Locale locale = super.getLocale();
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		datas = userCareLineApplication.findCareLineVO(user.getLocalUserId());
		if(locale.toString().equalsIgnoreCase("en_US")){
			if(datas.size()>0){
				I18nUtil.carLineI18N(datas, locale);
			}
		}
		language=I18nUtil.showLanguage(locale);
		return SUCCESS;
	}
	/**
	 * 删除我的关注航线
	 * @return
	 */
	@Action(value="delete",
			results={@Result(name="success", type="json", params={"root","returnBean"})})
	public String delete(){
		returnBean = new JsonReturnBean();
		try{
			userCareLineApplication.delete(id);
			returnBean.setHasOk(true);
		}catch(Exception e){
			returnBean.setHasOk(false);
			returnBean.setMessage(e.getMessage());
		}
		return SUCCESS;
	}
	/**
	 * 删除我的关注航线
	 * @return
	 */
	@Action(value="save",
			results={@Result(name="success", type="json", params={"root","returnBean"})})
	public String save(){
		returnBean = new JsonReturnBean();
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		try{
			PortEntityVO fromPortVO = this.portApplication.findCityNameByPortCode(depCity);
			if(fromPortVO == null){
				throw new ValidateBusiDataException("出发城市没有在系统中配置，请联系系统管理员。");
			}
			PortEntityVO toPortVO = this.portApplication.findCityNameByPortCode(arrCity);
			if(toPortVO == null){
				throw new ValidateBusiDataException("到达城市没有在系统中配置，请联系系统管理员。");
			}
			UserCareLineEntityVO vo = new UserCareLineEntityVO(
					user.getLocalUserId(), user.getUserName(),
					fromPortVO.getCityName(),toPortVO.getCityName(),fromPortVO.getPortCode(), toPortVO.getPortCode(), 0,0);
			userCareLineApplication.save(vo);
			returnBean.setHasOk(true);
		}catch(Exception e){
			returnBean.setHasOk(false);
			returnBean.setMessage(e.getMessage());
		}
		return SUCCESS;
	}

	public List<CityLineSelectVO> getTempLines() {
		return tempLines;
	}

	public void setTempLines(List<CityLineSelectVO> tempLines) {
		this.tempLines = tempLines;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
	
}
